import {timeFormat,getDayStartTime,testNumber} from '@/common/utils'
import type { ReceiveMsg} from '@/common/utils/IM'
import type {SaasIM} from '@/common/utils/IM/IM'
import type {IMOrderItem,GoodsItem} from '@/common/request/api'

const nowYear = new Date().getFullYear()

export const timeFrom = (timestamp:string|number|null,yesterday:string) => {
		if (timestamp == null) timestamp = Number(new Date());
		if (!testNumber(timestamp)) timestamp = Number(new Date(timestamp));
		timestamp = parseInt(`${timestamp}`);
		// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
		if (timestamp.toString().length == 10) timestamp *= 1000;
		let timer = new Date().getTime() - timestamp;
		timer = parseInt(`${timer / 1000}`);
		// 如果小于5分钟,则返回"刚刚",其他以此类推
		let tips = "";
		switch (true) {
			case timeFormat(new Date(),'yyyy-mm-dd') === timeFormat(new Date(timestamp),'yyyy-mm-dd') :
				tips = timeFormat(timestamp,'hh:MM')
				break;
			case getDayStartTime() - getDayStartTime(timestamp) <= 86400000:
				tips = `${yesterday} ${timeFormat(timestamp,'hh:MM')}`
				break;
			default:
			if(new Date(timestamp).getFullYear() !== nowYear){
				tips = timeFormat(timestamp,'yyyy-mm-dd hh:MM')
			}else{
				tips = timeFormat(timestamp,'mm-dd hh:MM')
			}
		}
		return tips;
	};
	
export type HistoryChatMsg = {
		time:number|null,
		data:ReceiveMsg[]
	}
export const getHistoryChatMsgList = (curChatMsg:ReceiveMsg[],historyChatMsgList:HistoryChatMsg[]) => {
	if(curChatMsg.length){
		let lastMsg:HistoryChatMsg = {
			time:null,
			data:[]
		}
		
		if(historyChatMsgList.length > 0){
			lastMsg = historyChatMsgList[historyChatMsgList.length-1]
		}
		curChatMsg.forEach(item => {
			let newTime = timeFormat(item.time,'yyyy-mm-dd hh:MM')
			if(timeFormat(lastMsg.time,'yyyy-mm-dd hh:MM') !== newTime){
				let newLastMsg = {
					 time:item.time,
					 data:[item]
				 }
				 historyChatMsgList.push(newLastMsg)
				 lastMsg = newLastMsg
			}else{
				lastMsg.data.push(item)
			}
		})
		historyChatMsgList.splice(historyChatMsgList.length-1,1,lastMsg)
	}
	return historyChatMsgList
}

export const sendOrder =  (IM:SaasIM,chatlinkid:string,unitEn:string,order:IMOrderItem) => {
		let msg = new IM.message('WxFriendChatReq',{
			body: {
				chatlinkid,
				thirdJson: JSON.stringify({
					createTime: order.createTime,
					deputys: [{
						commodityCount: order.commodityCount,
						commodityName: order.commodityName,
						commoditySpecName:order.commoditySpecName,
						commodityUrl: order.commodityUrl,
						deputyMoneyPay:order.deputyMoney
					}],
					orderId: order.id,
					orderMoneyPay: `${unitEn}${order.deputyMoneyPay}`,
					type: "2"
				})
			}
		})
		IM.send(msg)
	}
export const sendGoods =  (IM:SaasIM,chatlinkid:string,unitEn:string,goods:GoodsItem) => {
		let msg = new IM.message('WxFriendChatReq',{
			body: {
				chatlinkid,
				thirdJson:JSON.stringify({
					pId: goods.id,
					pType: goods.type,
					price: `${unitEn}${goods.price}`,
					productImage: goods.coverImg,
					productName: goods.name,
					type: "1"
				})
			}
		})

		IM.send(msg)
	}

// #ifdef APP-PLUS
const platform = plus.os.name
const audioContext  = plus.audio.createPlayer({
    src: '/pages/public/static/newMsg.mp3'
});
audioContext.setSessionCategory('ambient');
// #endif
// #ifndef APP-PLUS
const audioContext  = uni.createInnerAudioContext();
audioContext.src = '/pages/public/static/newMsg.mp3';

// #endif


export const audioPlay = () => {
	// #ifdef APP-PLUS
	// android需判断系统音量才播放音频,ios下可直接播放
	if (platform == 'Android') {
	    let volume = plus.device.getVolume();
	    if (volume === 0) {
	        return;
	    }
	}
	
	// #endif
	audioContext.play();
}